<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
         * 手写深拷贝方法
        */
        const obj = {
            name:'张三',
            info:{
                name:undefined,
                fn:()=>{}
            }
        }
        console.log(JSON.parse(JSON.stringify(obj)))
        console.log({...obj})
        console.log(Object.assign({},obj))
        function deepClone(data){
            if(typeof data !== 'object' || data === null) return data
            const result = Array.isArray(data) ? [] : {}
            for(let key in data){
                result[key] = deepClone(data[key])
            }
            return result
        }

       //手写new过程

       function myNew(fn,...args){
        const obj = {}
        obj._proto_ = fn.prototype
        const result = fn.call(obj,...args)
        return result instanceof Object ? result : obj
       }
      //手写call,bind,apply
      Function.prototype.myCall = function(obj,...args){
        const cxt = obj ?? globalThis
        const key = Symbol('call')
        cxt[key] = this
        const result = cxt[key](...args)
        delete cxt[key]
        return result
      }
      Function.prototype.myApply = (obj,params)=>{
        const cxt = obj ?? globalThis
        const key = Symbol('apply')
        cxt[key] = this
        const result = cxt[key](...params)
        delete cxt[key]
        return result
      }
      Function.prototype.myBind = (obj,...args)=>{
        const _this = this
        return function(...params){
            _this.call(obj,...args,...params)
        }
      }
      test.myApply(obj,[params1,params2,params])
      test.myCall(obj,params1,params2,params)
     //手写防抖、节流
     function myDep(fn,delay){
        let timer
        return function(...args){
            clearTimeout(timer)
            timer = setTimeout(()=>{
                fn.call(this,...args)
            },delay)
        }
     }


     function myThr(fn,delay){
        let lastTime = 0
        return function(...args){
            const nowTime = Date.now()
            if(nowTime - lastTime >= delay){
                fn.call(this,...args)
                lastTime = nowTime
            }
        }
     }
    </script>
</body>

</html>